using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MatrixFramework.Shared.Contracts.Security
{
    /// <summary>
    /// 安全相关合约
    /// 可被所有服务重用的安全功能
    /// </summary>

    // 敏感数据保护
    public interface ISensitiveDataProtectionAppService
    {
        Task<ProtectedDataDto> ProtectDataAsync(ProtectDataInput input);
        Task<UnprotectedDataDto> UnprotectDataAsync(UnprotectDataInput input);
        Task<SensitiveDataScanResultDto> ScanForSensitiveDataAsync(SensitiveDataScanInput input);
    }

    // API密钥管理
    public interface IApiKeyManagementAppService
    {
        Task<ApiKeyDto> CreateApiKeyAsync(CreateApiKeyInput input);
        Task<bool> ValidateApiKeyAsync(string apiKey, string resource);
        Task<ApiKeyDto> GetApiKeyAsync(string keyId);
        Task<bool> RevokeApiKeyAsync(string keyId);
        Task<List<ApiKeyDto>> GetApiKeysAsync(string clientId);
    }

    // 审计日志
    public interface IAuditLogAppService
    {
        Task LogAuditEventAsync(AuditLogEventDto auditEvent);
        Task<PagedResultDto<AuditLogEntryDto>> GetAuditLogsAsync(GetAuditLogsInput input);
        Task<AuditLogStatisticsDto> GetAuditStatisticsAsync(GetAuditStatisticsInput input);
    }

    // DTO定义
    public class ProtectedDataDto
    {
        public string ProtectedData { get; set; } = string.Empty;
        public string Algorithm { get; set; } = string.Empty;
        public DateTime ProtectionTime { get; set; }
        public Dictionary<string, string> Metadata { get; set; } = new();
    }

    public class ProtectDataInput
    {
        public string Data { get; set; } = string.Empty;
        public string Purpose { get; set; } = string.Empty;
        public List<string> DataCategories { get; set; } = new();
        public Dictionary<string, string> Metadata { get; set; } = new();
    }

    public class UnprotectedDataDto
    {
        public string Data { get; set; } = string.Empty;
        public DateTime UnprotectionTime { get; set; }
        public bool IsSuccess { get; set; }
        public string? ErrorMessage { get; set; }
    }

    public class UnprotectDataInput
    {
        public string ProtectedData { get; set; } = string.Empty;
        public string Purpose { get; set; } = string.Empty;
    }

    public class SensitiveDataScanResultDto
    {
        public bool ContainsSensitiveData { get; set; }
        public List<SensitiveDataFindingDto> Findings { get; set; } = new();
        public DateTime ScanTime { get; set; }
        public string DataHash { get; set; } = string.Empty;
    }

    public class SensitiveDataFindingDto
    {
        public string DataType { get; set; } = string.Empty;
        public string Pattern { get; set; } = string.Empty;
        public string Location { get; set; } = string.Empty;
        public int StartIndex { get; set; }
        public int Length { get; set; }
        public double ConfidenceScore { get; set; }
        public string? Suggestion { get; set; }
    }

    public class SensitiveDataScanInput
    {
        public string Data { get; set; } = string.Empty;
        public List<string> ScanTypes { get; set; } = new();
        public bool IncludeContext { get; set; } = true;
    }

    public class ApiKeyDto
    {
        public string KeyId { get; set; } = string.Empty;
        public string ClientId { get; set; } = string.Empty;
        public string KeyName { get; set; } = string.Empty;
        public List<string> Permissions { get; set; } = new();
        public List<string> Resources { get; set; } = new();
        public DateTime CreatedAt { get; set; }
        public DateTime? ExpiresAt { get; set; }
        public DateTime? LastUsedAt { get; set; }
        public bool IsActive { get; set; }
        public int UsageCount { get; set; }
    }

    public class CreateApiKeyInput
    {
        public string ClientId { get; set; } = string.Empty;
        public string KeyName { get; set; } = string.Empty;
        public List<string> Permissions { get; set; } = new();
        public List<string> Resources { get; set; } = new();
        public DateTime? ExpiresAt { get; set; }
        public int? UsageLimit { get; set; }
    }

    public class AuditLogEventDto
    {
        public string EventType { get; set; } = string.Empty;
        public string Resource { get; set; } = string.Empty;
        public string Action { get; set; } = string.Empty;
        public string? UserId { get; set; }
        public string? TenantId { get; set; }
        public string? ClientId { get; set; }
        public string? IpAddress { get; set; }
        public string? UserAgent { get; set; }
        public Dictionary<string, object> Properties { get; set; } = new();
        public bool IsSuccess { get; set; }
        public string? ErrorMessage { get; set; }
        public DateTime Timestamp { get; set; }
    }

    public class PagedResultDto<T>
    {
        public long TotalCount { get; set; }
        public List<T> Items { get; set; } = new();
    }

    public class GetAuditLogsInput
    {
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public string? EventType { get; set; }
        public string? Resource { get; set; }
        public string? UserId { get; set; }
        public string? TenantId { get; set; }
        public bool? IsSuccess { get; set; }
        public int MaxResultCount { get; set; } = 50;
        public int SkipCount { get; set; } = 0;
    }

    public class AuditLogEntryDto
    {
        public string Id { get; set; } = string.Empty;
        public string EventType { get; set; } = string.Empty;
        public string Resource { get; set; } = string.Empty;
        public string Action { get; set; } = string.Empty;
        public string? UserId { get; set; }
        public string? TenantId { get; set; }
        public string? ClientId { get; set; }
        public string? IpAddress { get; set; }
        public string? UserAgent { get; set; }
        public Dictionary<string, object> Properties { get; set; } = new();
        public bool IsSuccess { get; set; }
        public string? ErrorMessage { get; set; }
        public DateTime Timestamp { get; set; }
    }

    public class GetAuditStatisticsInput
    {
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public string? TenantId { get; set; }
        public List<string> EventTypes { get; set; } = new();
    }

    public class AuditLogStatisticsDto
    {
        public long TotalEvents { get; set; }
        public long SuccessEvents { get; set; }
        public long FailureEvents { get; set; }
        public Dictionary<string, long> EventTypeCounts { get; set; } = new();
        public Dictionary<string, long> ResourceCounts { get; set; } = new();
        public Dictionary<string, long> UserActivityCounts { get; set; } = new();
        public List<DailyActivityDto> DailyActivities { get; set; } = new();
    }

    public class DailyActivityDto
    {
        public DateTime Date { get; set; }
        public long EventCount { get; set; }
        public long SuccessCount { get; set; }
        public long FailureCount { get; set; }
    }
}